package com.binguo.thirdPay;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * @author yuritian
 * @Title: AESUtil
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2019-07-2714:41
 */
public class AESUtil {

    private static final String cKey = "szFz2pZsy3bAqKJy";

    private static final String Key = "IR8cQ9wzm46NJ81RSWN9FjOPqdWruVls";

    private static final String sh1Key = "d9CXVjmdl3Xz5mfpZWqKOEdqTjBrVyX8";


    public static void main(String[] args) {
        try {
            //System.out.println(AESUtil.getAESBase64("{\"cmd\":10002,\"data\":{\"auth\":0,\"qrcode\":\"{\\\"id\\\":47,\\\"check\\\":2,\\\"time\\\":\\\"2019-07-25 11:11:36\\\"}\\r\",\"type\":2},\"dsn\":\"380a30fb3fd1d762b7cb2bc43bcec2f5\"}"));
            //System.out.println(AESUtil.desEncrypt("c3pGejJwWnN5M2JBcUtKeYZY/B4QXS+4tmp42k1xHJc"));
            System.out.println(encrypt(""));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //加密
    public static String encrypt(String data) throws Exception {
        try {
            Base64 base64 = new Base64();
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] plaintext = data.getBytes();
            SecretKeySpec keyspec = new SecretKeySpec(Key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(cKey.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
            System.out.println(base64.encodeToString(encrypted));
            byte[] ckey = cKey.getBytes();
            byte[] result = new byte[ckey.length+encrypted .length];
            System.arraycopy(ckey, 0, result, 0, ckey.length);
            System.arraycopy(encrypted, 0, result, ckey.length, encrypted.length);
            return base64.encodeToString(result);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //获得待签名字符串
    public static String getAESBase64(String data) throws Exception {
        System.out.println(data);
        Base64 base64 = new Base64();
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] plaintext = data.getBytes();
        SecretKeySpec keyspec = new SecretKeySpec(Key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(cKey.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
        byte[] encrypted = cipher.doFinal(plaintext);
        System.out.println(base64.encodeToString(encrypted));
        return cKey+base64.encodeToString(encrypted)+sh1Key;
    }
    //解密
    public static String desEncrypt(String data) throws Exception {
        try
        {
            Base64 base64 = new Base64();
            byte[] encrypted1 = base64.decode(data);
            encrypted1 = Arrays.copyOfRange(encrypted1,16,encrypted1.length);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keyspec = new SecretKeySpec(Key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(cKey.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
